#!/bin/bash -il

DEST_DIR="dest"

APPLICATION_NAME=config
IMAGE_NAME=${APPLICATION_NAME}
SERVICE_NAME=${APPLICATION_NAME}-server
EXECUTE_JAR=${SERVICE_NAME}.jar

PORT=10000

SECURITY_NAME="demon"
SECURITY_PASSWORD="demon"
GIT_URL="https://gitee.com/just3019/cloudresources.git"
GIT_USERNAME="455674848@qq.com"
GIT_PASSWORD="12345678a"
CONFIG_PROFILE="dev"


BIND_DIR_ROOT=/data/containers

# 打包项目，并将jar拷贝到dest目录中
function mvn_package(){
    # 打包项目
    echo "打包项目: mvn -U clean compile package -Dmaven.test.skip=true"
    mvn -U clean compile package -Dmaven.test.skip=true
    # 判断存放打包文件目录是否为空
    if test -z "${DEST_DIR}"; then
        echo "存放打包文件目录为空"
        exit 1
    else
        rm -fr ${DEST_DIR}/*
        mkdir -pv ${DEST_DIR}
        cp -r scripts/* ${DEST_DIR}/
    fi
    # jar 目录
    ORG_PATH=${SERVICE_NAME}/target/${EXECUTE_JAR}
    # -f 判断${ORG_PATH}是否存在
    if [[ -f "${ORG_PATH}" ]];
    then
        cp ${ORG_PATH} ${DEST_DIR}/${EXECUTE_JAR}
        echo "success"
    else
        echo "打包失败"
        exit 1
    fi
}

# 打包docker镜像
function docker_build(){
    echo "开始打包镜像"
    docker build -t ${IMAGE_NAME} .
    echo "完成镜像打包"
}

#get container name
function container_name(){
    CONTAINER_NAME=${APPLICATION_NAME}-${CONFIG_PROFILE}-${PORT}
}

#get bind_dir path
function bind_dir_path(){
    BIND_DIR=${BIND_DIR_ROOT}/${APPLICATION_NAME}-${CONFIG_PROFILE}-${PORT}
}

# run a docker container with image
function docker_run(){
    container_name
    bind_dir_path
    docker run -ti -d \
    --net host \
    --name ${CONTAINER_NAME} \
    --restart always \
    -v ${BIND_DIR}/logs:/data/logs \
    --env PORT=${PORT} \
    --env SECURITY_USERNAME=${SECURITY_NAME} \
    --env SECURITY_PASSWORD=${SECURITY_PASSWORD} \
    --env GIT_URL=${GIT_URL} \
    --env GIT_USERNAME=${GIT_USERNAME} \
    --env GIT_PASSWORD=${GIT_PASSWORD} \
    --env APPLICATION_NAME=${APPLICATION_NAME} \
    --env EXECUTE_JAR=${EXECUTE_JAR} \
    ${IMAGE_NAME}
}

# 删除冲突的容器，根据容器名和端口查找冲突
function docker_rm_containers(){
    container_name
    echo "${CONTAINER_NAME}|${PORT}"
    docker ps -a | grep -E "${CONTAINER_NAME}|${PORT}" | grep -v grep
    if [[ $? -ne 0 ]]; then
        echo "没有需要删除的容器"
    else
        echo "有需要删除的容器"
        docker ps -a |grep -E "${CONTAINER_NAME}|${PORT}" |awk '{print $1}' | xargs docker stop
        docker ps -a |grep -E "${CONTAINER_NAME}|${PORT}" |awk '{print $1}' | xargs docker rm -f
        echo "容器删除完成"
    fi
}

# 删除tag为none的镜像
function docker_rm_images(){
    docker images | grep none | grep -v grep
    if [[ $? -ne 0 ]]
    then
        echo "没有镜像需要删除"
    else
        echo "开始删除镜像"
        docker images | grep none | awk '{print $3}' | xargs docker rmi
        echo "完成删除镜像"
    fi

}

function deploy_in_docker(){
    IN_PROFILE=$1
    IN_PORT=$2
    # 更新代码
    git pull
    # 打包并拷贝文件
    mvn_package
    # 设置profile
    if test -z "${IN_PROFILE}"; then
        echo "profile没有自定义，使用默认dev"
    else
        CONFIG_PROFILE=${IN_PROFILE}
    fi
    # 设置端口
    if test -z "${IN_PORT}"; then
        echo "port没有自定义，使用默认"
    else
        PORT=${IN_PORT}
    fi
    # 进入打包工作空间
    cd ${DEST_DIR}
    # 打包镜像
    docker_build
    # 关闭旧容器
    docker_rm_containers
    # 删除none镜像
    docker_rm_images
    # 启动容器
    docker_run
}

